package com.zdkj.subway.check.service;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zdkj.subway.check.entity.SwAuTvmBoxAmountDifference;
import com.zdkj.subway.check.enums.ResultDetailMsg;
import com.zdkj.subway.check.mapper.SwAuTvmBoxAmountDifferenceMapper;
import com.zdkj.subway.check.result.Result;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 报表差错信息
 */
@Service
public class SwAuTvmBoxAmountDifferenceService extends ServiceImpl<SwAuTvmBoxAmountDifferenceMapper, SwAuTvmBoxAmountDifference> {

    @Resource
    private SwAuTvmBoxAmountDifferenceMapper swAuTvmBoxAmountDifferenceMapper;

    /**
     * TVM票款差异分钱箱差异审核 列表
     * @param jsonObject
     * @return
     */
    public Result getSwAuConductorNoticeList(JSONObject jsonObject) {
        //分页
        Integer startPage = jsonObject.getInteger("currentPage");
        Integer pageSize = jsonObject.getInteger("pageSize");
        //收入时间
        Date startTime = jsonObject.getDate("startTime");
        Date endTime = jsonObject.getDate("endTime");
        //TVM编号
        String tvmCode = jsonObject.getString("tvmCode");
        //卸载状态
        String boxUninstallStatus = jsonObject.getString("boxUninstallStatus");
        Example example = new Example(SwAuTvmBoxAmountDifference.class);
        Example.Criteria criteria = example.createCriteria();
        if (!StringUtils.isEmpty(tvmCode)) {
            criteria.andEqualTo("tvmCode", tvmCode);
        }
        if (!StringUtils.isEmpty(boxUninstallStatus)) {
            criteria.andEqualTo("boxUninstallStatus", boxUninstallStatus);
        }
        if (startTime != null && endTime != null) {
            criteria.andGreaterThanOrEqualTo("incomeSettleBeginTime",startTime);
            criteria.andLessThanOrEqualTo("incomeSettleEndTime",endTime);
        }
        PageHelper.startPage(startPage,pageSize);
        List<SwAuTvmBoxAmountDifference> swAuTvmBoxAmountDifferences =
                swAuTvmBoxAmountDifferenceMapper.selectByExample(example);
        PageInfo<SwAuTvmBoxAmountDifference> swAuTvmBoxAmountDifferencePageInfo
                = new PageInfo<>(swAuTvmBoxAmountDifferences);
        JSONObject resultJson = new JSONObject();
        resultJson.put("list", swAuTvmBoxAmountDifferences);
        resultJson.put("pageInfo", swAuTvmBoxAmountDifferencePageInfo);
        return new Result(ResultDetailMsg.OK, resultJson);
    }

    /**
     * TVM票款差异分钱箱差异审核 单独查询
     * @param jsonObject
     * @return
     */
    public Result getSwAuConductorNotice(JSONObject jsonObject) {
        Integer id = jsonObject.getInteger("id");
        SwAuTvmBoxAmountDifference swAuTvmBoxAmountDifference = swAuTvmBoxAmountDifferenceMapper.selectByPrimaryKey(id);
        JSONObject resultJson = new JSONObject();
        resultJson.put("obj",swAuTvmBoxAmountDifference);
        return new Result(ResultDetailMsg.OK,resultJson);
    }

    /**
     * TVM票款差异分钱箱差异审核 修改
     * @param jsonObject
     * @return
     */
    public Result updateSwAuConductorNotice(JSONObject jsonObject) {
        SwAuTvmBoxAmountDifference swAuTvmBoxAmountDifference =
                JSONObject.parseObject(jsonObject.toJSONString(), SwAuTvmBoxAmountDifference.class);
        int i = swAuTvmBoxAmountDifferenceMapper.updateByPrimaryKeySelective(swAuTvmBoxAmountDifference);
        return new Result(ResultDetailMsg.OK);
    }

    /**
     * 批量修改差异原因
     * @param param
     * @return
     */
    public Result updateDifferences(Map<String, String> param) {
        List<SwAuTvmBoxAmountDifference> list = new ArrayList<>();
        List<Integer> ids = (List)JSONObject.parseArray(String.valueOf(param.get("ids")));
        String diffReason = String.valueOf(param.get("diffReason"));
        //根据id批量修改差异原因
        for(Integer id : ids){
            SwAuTvmBoxAmountDifference swAuTvmBoxAmountDifference = new SwAuTvmBoxAmountDifference();
            swAuTvmBoxAmountDifference.setId(Integer.valueOf(id));
            swAuTvmBoxAmountDifference.setDiffReason(diffReason);
            list.add(swAuTvmBoxAmountDifference);
        }
        updateBatchById(list);
        return new Result(ResultDetailMsg.OK);
    }
}
